import find from 'lodash/find';
const initPathName = `/${window.location.pathname.split('/')[1]}`;
const routerMapping = [
  { path: '/fire_hydrant', name: '设备列表', modelName: 'pressure_sensors' },
  { path: '/fire_hydrant_alarm', name: '报警列表', modelName: 'fire_hydrant_alarm' },
  { path: '/fire_hydrant_log', name: '日志列表', modelName: 'fire_hydrant_log' },
  { path: '/hydrant_inspection_records', name: '巡检记录', modelName: 'hydrant_inspection_records' },
  { path: '/concentrator_firmwares', name: '集中器固件列表', modelName: 'concentrator_firmwares' },
  { path: '/roles', name: '角色管理', modelName: 'roles' },
  { path: '/permissions', name: '权限管理', modelName: 'permissions' },
  { path: '/staffs', name: '账号管理', modelName: 'staffs' },
  { path: '/device_map', name: '设备地图', modelName: 'meters_map' },

];
const exit = find(routerMapping, o => o.path == initPathName);

export default {
  namespace: 'global',

  state: {
    collapsed: false,
    notices: [],
    task_id:'0',
    routerMapping:routerMapping,
    activeKey:initPathName,
    gridTableW:100,
    compare_modal:false,
    compare_data_modal:false,
    compare_data:[],
    panes: (initPathName === '/device_map' || !exit) ? [{ title: '设备地图', key: '/device_map' }] : [{
      title: '设备地图',
      key: '/device_map',
    }, {
      title: exit.name, key: initPathName,
    }],
  },

  effects: {
    *changeTaskId({ payload }, { call, put }) {
      console.log('修改配置返回task_id',payload)
      yield put({
        type: 'saveTaskId',
        payload: payload,
      });
    },
    *changeOneProperty({ payload, callback }, { call, put }) {
      yield put({
        type: 'saveOneProperty',
        payload:payload
      });
      setTimeout(function() {
        if(callback) callback()
      },0)

    },
    *getGridTableW({ payload }, { call, put }) {
      let topTab=document.querySelector('.body-sidar')
      if(topTab){
        let w=document.body.offsetWidth-topTab.offsetWidth-22;
        yield put({
          type: 'saveGridTableW',
          payload: w,
        });
      }
    },
    *changePanesAndActive({ payload,callback  }, { call, put }) {
      console.log('payload',payload)
      yield put({
        type: 'savePanesAndActive',
        payload: payload,
      });
      if(callback)callback()
    },
    *changeActive({ payload }, { call, put }) {
      console.log('payload',payload)
      yield put({
        type: 'savePanesAndActive',
        payload: payload,
      });
    },
    *jumpToTab({ payload,callback }, { call, put ,select}) {
      console.log('payload',payload);
      let {panes} = yield select(state => state.global);
      const exit = find(panes, o => o.key == payload.activeKey);
      console.log('exit',exit)
      if(exit){
        console.log('tab已经存在')
        yield put({
          type: 'savePanesAndActive',
          payload: {
            activeKey:payload.activeKey
          },
        });
      }else{
        console.log('tab不存在');
        panes.push({
          title: find(routerMapping, o => o.path == payload.activeKey).name,
          key: payload.activeKey,
        });
        yield put({
          type: 'savePanesAndActive',
          payload: {
            activeKey:payload.activeKey,
            panes
          },
        });
      }
      if(callback)callback()
    },
  },

  reducers: {
    saveOneProperty(state, action) {
      return {
        ...state,
        ...action.payload
      };
    },
    saveGridTableW(state, { payload }) {
      return {
        ...state,
        gridTableW:payload
      };
    },
    savePanesAndActive(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    changeLayoutCollapsed(state, { payload }) {
      return {
        ...state,
        collapsed: payload,
      };
    },
    saveTaskId(state, action) {
      return {
        ...state,
        task_id: action.payload,
      };
    },
  },
};
